AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')
local Ground = 1 + 0 + 2 + 8 + 32
local rel = "hl1/ambience/labdrone2.wav"
util.PrecacheSound(rel)

function ENT:Initialize()
	self.active = true
	self.Entity:SetModel( "models/props_c17/canister01a.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType(  MOVETYPE_VPHYSICS   )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:SetName("Capital.Ship.Steering.Unit")
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
	self.Angles = self.Entity:GetAngles()
	self.Physik = phys
	self.Pos = self.Entity:GetPos()
	self.maxpower = 300
	self.power = 0
	phys:SetMass( 50000 )
	self.Entity:StartMotionController()
	table.insert(servertable["ship"]["controller"],self.Entity)
	self.done = false
end

function ENT:OnRemove()
	if self.done then
		if self.core:IsValid() then
			self.core.maxcapaciator = self.core.maxcapaciator -4000
			self.core.maxshield = self.core.maxshield - 10000
			self.core.maxhealth = self.core.maxhealth - 10000
			self.core.maxenergy = self.core.maxenergy - 200
			self.core.maxshield = self.core.maxshield - 5000
			self.core.maxbullets = self.core.maxbullets - 150
			self.core.maxrockets = self.core.maxrockets - 15
			self.core.maxturrets = self.core.maxturrets - 3
			self.core.maxcapaciator = self.core.maxcapaciator - 4000
			self.core.maxcpu = self.core.maxcpu - 5

		end
	end
end

function ENT:Think()
	self.Entity:NextThink(CurTime() + 1)
	self:EmitSound(rel,250,math.Clamp(self.power,10,200))
	if self.core then
		if  (self.core:IsValid()) then	
			self.core.heat = self.core.heat - 5
			self.core.energy = self.core.energy + 10
			self.core.capaciator = self.core.capaciator + 20
			self.core.shield = self.core.shield +20
			if self.done then
				self.Entity:NextThink( CurTime() + 30 )
			else
				self.core.maxshield = self.core.maxshield + 45000
				self.core.maxhealth = self.core.maxhealth + 50000
				self.core.maxenergy = self.core.maxenergy + 1000
				self.core.health = self.core.maxhealth
				self.core.shield = self.core.maxshield
				self.core.maxbullets = self.core.maxbullets + 500
				self.core.maxrockets = self.core.maxrockets + 15
				self.core.maxturrets = self.core.maxturrets + 3
				self.core.maxcapaciator = self.core.maxcapaciator + 7500
				self.core.maxcpu = self.core.maxcpu + 5
				self.done = true
			end
		else
		self.core = nil
		end
	else
		self.core = getshipcore(self.Entity)
		Msg (tostring(self.core) .. "\n")
		if not self.core then
			self.Entity:NextThink( CurTime() + 5 )
		end
	end
return true
end

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "Test2" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:PhysicsSimulate() 
 	self.Physik:Wake()
	
	self.ShadowParams={} 
	self.ShadowParams.secondstoarrive	= 5
	self.ShadowParams.pos			= self.Pos
	self.ShadowParams.angle			= self.Angles
	self.ShadowParams.maxangular		= 5000
	self.ShadowParams.maxangulardamp	= 10000
	self.ShadowParams.maxspeed		= 1000000
	self.ShadowParams.maxspeeddamp		= 10000
	self.ShadowParams.dampfactor		= 0.8
	self.ShadowParams.teleportdistance	= 0
	self.ShadowParams.deltatime		= 1
   
	self.Physik:ComputeShadowControl(self.ShadowParams) 
   
 end  
 
 function ENT:OnTakeDamage(info)
	Shieldhit(info,self.Entity)
end